package com.yaodian100.ec.cobranding.web;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.cobranding.entity.Hao123;
import com.yaodian100.ec.cobranding.entity.Hao123Coordinates;
import com.yaodian100.ec.cobranding.entity.Marketingxml;
import com.yaodian100.ec.cobranding.service.Hao123Service;
import com.yaodian100.ec.cobranding.service.MarketingXMLService;

public class Hao123Action extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -558055313126798253L;
	@Resource
	MenuService menuService;
	@Resource
	Hao123Service hao123Service;
	@Resource
	MarketingXMLService mktXMLService;

	private String loc;
	private String mainWebsite;
	private List<Hao123> hao123s;
	private String website;
	private String activityurl;
	private String category;
	private String keyword1;
	private String keyword2;
	private String keyword3;
	private String major;
	private String title;
	private String sampleimage;
	private String image;
	private String startDateParam;
	private String timestart;
	private String endDateParam;
	private String timeend;
	private List<Hao123Coordinates> hao123Cos;
	private String[] x1;
	private String[] y1;
	private String[] x2;
	private String[] y2;
	private String[] coTitle;
	private String[] href;
	private Menu categoryMenu;
	private Hao123 hao123;
	private String detailId;
	private File[] imageSmall;
	private File[] imageBig;
	private String xmlResult;
	private String imageSPath;
	private String imageBPath;
	private String xmlLastModify;

	public String hao123Main() {
		HashMap<String, String> hao123Main = hao123Service.getHao123Main();
		if (hao123Main.containsKey("loc")) {
			loc = hao123Main.get("loc");
		}
		if (hao123Main.containsKey("website")) {
			// mainWebsite = hao123Main.get("website");
			mainWebsite = "耀点100百货商城";// 限制固定值
		}
		if (hao123Main.containsKey("lastModify")) {
			xmlLastModify = hao123Main.get("lastModify");
		} else {
			xmlLastModify = "不明";
		}
		hao123s = hao123Service.getHao123();
		return "hao123Main";
	}

	public String addHao123() {
		return "edit";
	}

	public String editHao123Detail() {
		hao123 = new Hao123();
		hao123 = hao123Service.getHao123Deatil(detailId);
		if (null != hao123) {
			hao123Cos = hao123.getSet();
		}
		// 处理图路径
		if (StringUtils.isNotBlank(hao123.getSampleimage())) {// 选填栏位
			imageSPath = StringUtils.replace(hao123.getSampleimage(), ServerValue.getImgDomain(), ServerValue
					.getMidHostname());
		}
		imageBPath = StringUtils.replace(hao123.getImage(), ServerValue.getImgDomain(), ServerValue.getMidHostname());

		return "edit";
	}

	public String saveHao123Detail() throws Exception {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date startTime = df.parse(startDateParam + " " + timestart);
		Date endTime = df.parse(endDateParam + " " + timeend);
		// 準備圖檔
		String tempFileName = String.valueOf(new Date().getTime());
		String imageSmallName = "";
		String imageBigName = "";
		if (null != imageSmall) {
			BufferedImage bufferedImage = ImageIO.read(imageSmall[0]);
			if (bufferedImage.getHeight() == 150 && bufferedImage.getWidth() == 212 && (imageSmall[0].length() / 1024) <= 30) {
				imageSmallName = uploadImageFile(imageSmall, tempFileName + "s.jpg");
			} else {
				this.addActionError("活动海报小图<sampleimage>图片规格不符合规范，请检查图片尺寸需為大小30以內，尺寸為寬212 x  高150。");
				if (StringUtils.isNotBlank(detailId)) {
					return editHao123Detail();
				} else {
					return "edit";
				}
			}

		} else {
			imageSmallName = sampleimage;
		}
		if (null != imageBig) {
			imageBigName = uploadImageFile(imageBig, tempFileName + "b.jpg");
		} else {
			imageBigName = image;
		}

		List<Hao123Coordinates> hao123Cos = new ArrayList<Hao123Coordinates>();
		if (null != x1) {// 选填栏位
			for (int i = 0; i < x1.length; i++) {
				Hao123Coordinates hao123co = new Hao123Coordinates();
				hao123co.setX1(x1[i]);
				hao123co.setY1(y1[i]);
				hao123co.setX2(x2[i]);
				hao123co.setY2(y2[i]);
				hao123co.setTitle(coTitle[i]);
				hao123co.setHref(href[i]);
				hao123Cos.add(hao123co);

			}
		}

		Hao123 newHao123 = new Hao123();
		newHao123.setWebsite(website);
		newHao123.setActivityurl(activityurl);
		newHao123.setCategory(category);
		newHao123.setKeyword1(keyword1);
		if (StringUtils.isNotBlank(keyword2)) {// 选填栏位
			newHao123.setKeyword2(keyword2);
		}
		if (StringUtils.isNotBlank(keyword3)) {// 选填栏位
			newHao123.setKeyword3(keyword3);
		}
		newHao123.setMajor(major);
		newHao123.setTitle(title);
		if (StringUtils.isNotBlank(imageSmallName)) {// 选填栏位
			newHao123.setSampleimage(imageSmallName);
		}
		newHao123.setImage(imageBigName);
		newHao123.setStartTime(startTime);
		newHao123.setEndTime(endTime);
		newHao123.setSet(hao123Cos);

		hao123Service.saveHao123XML(detailId, newHao123);

		return hao123Main();
	}

	public String makeXML() {
		mainWebsite = "耀点100百货商城";// 限制固定值
		String xmlPath = hao123Service.makeCompleteXML(loc, mainWebsite);

		if (StringUtils.isNotBlank(xmlPath)) {
			xmlResult = "done";
		}
		return "jsomnXMLRes";
	}

	public String deleteHao123Detail() {
		Marketingxml mktXML = mktXMLService.get(detailId);
		if (null != mktXML) {
			mktXMLService.delete(mktXML);
		}

		return hao123Main();
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		if (categoryMenu == null) {
			categoryMenu = menuService.get("hao123.category");
		}

	}

	private String uploadImageFile(File[] file, String fileName) throws Exception {
		StringBuffer filePath = new StringBuffer(ServerValue.getMidApachePath()).append("3party_ex").append(File.separator)
				.append("hao123").append(File.separator);
		String completePath = filePath.append(fileName).toString();
		// 檢查資料夾
		File folder = new File(filePath.toString());
		if (!folder.exists()) {
			folder.mkdirs();
		}
		// 檢查檔案
		File f = new File(completePath.toString());
		if (f.exists()) {
			f.delete();
		}
		InputStream inputStream = new FileInputStream(file[0]);
		OutputStream out = new FileOutputStream(f);
		byte buf[] = new byte[1024];
		int len;
		while ((len = inputStream.read(buf)) > 0) {
			out.write(buf, 0, len);
		}
		out.close();
		inputStream.close();

		if (f.exists()) {
			StringBuffer httpPath = new StringBuffer(ServerValue.getImgDomain()).append("/3party_ex/hao123/")
					.append(fileName);
			return httpPath.toString();
		} else {
			return "";
		}

	}

	/**
	 * @return the loc
	 */
	public String getLoc() {
		return loc;
	}

	/**
	 * @param loc the loc to set
	 */
	public void setLoc(String loc) {
		this.loc = loc;
	}

	/**
	 * @return the mainWebsite
	 */
	public String getMainWebsite() {
		return mainWebsite;
	}

	/**
	 * @param mainWebsite the mainWebsite to set
	 */
	public void setMainWebsite(String mainWebsite) {
		this.mainWebsite = mainWebsite;
	}

	/**
	 * @return the hao123s
	 */
	public List<Hao123> getHao123s() {
		return hao123s;
	}

	/**
	 * @param hao123s the hao123s to set
	 */
	public void setHao123s(List<Hao123> hao123s) {
		this.hao123s = hao123s;
	}

	/**
	 * @return the website
	 */
	public String getWebsite() {
		return website;
	}

	/**
	 * @param website the website to set
	 */
	public void setWebsite(String website) {
		this.website = website;
	}

	/**
	 * @return the activityurl
	 */
	public String getActivityurl() {
		return activityurl;
	}

	/**
	 * @param activityurl the activityurl to set
	 */
	public void setActivityurl(String activityurl) {
		this.activityurl = activityurl;
	}

	/**
	 * @return the category
	 */
	public String getCategory() {
		return category;
	}

	/**
	 * @param category the category to set
	 */
	public void setCategory(String category) {
		this.category = category;
	}

	/**
	 * @return the keyword1
	 */
	public String getKeyword1() {
		return keyword1;
	}

	/**
	 * @param keyword1 the keyword1 to set
	 */
	public void setKeyword1(String keyword1) {
		this.keyword1 = keyword1;
	}

	/**
	 * @return the keyword2
	 */
	public String getKeyword2() {
		return keyword2;
	}

	/**
	 * @param keyword2 the keyword2 to set
	 */
	public void setKeyword2(String keyword2) {
		this.keyword2 = keyword2;
	}

	/**
	 * @return the keyword3
	 */
	public String getKeyword3() {
		return keyword3;
	}

	/**
	 * @param keyword3 the keyword3 to set
	 */
	public void setKeyword3(String keyword3) {
		this.keyword3 = keyword3;
	}

	/**
	 * @return the major
	 */
	public String getMajor() {
		return major;
	}

	/**
	 * @param major the major to set
	 */
	public void setMajor(String major) {
		this.major = major;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the sampleimage
	 */
	public String getSampleimage() {
		return sampleimage;
	}

	/**
	 * @param sampleimage the sampleimage to set
	 */
	public void setSampleimage(String sampleimage) {
		this.sampleimage = sampleimage;
	}

	/**
	 * @return the image
	 */
	public String getImage() {
		return image;
	}

	/**
	 * @param image the image to set
	 */
	public void setImage(String image) {
		this.image = image;
	}

	/**
	 * @return the hao123Cos
	 */
	public List<Hao123Coordinates> getHao123Cos() {
		return hao123Cos;
	}

	/**
	 * @param hao123Cos the hao123Cos to set
	 */
	public void setHao123Cos(List<Hao123Coordinates> hao123Cos) {
		this.hao123Cos = hao123Cos;
	}

	/**
	 * @return the x1
	 */
	public String[] getX1() {
		return x1;
	}

	/**
	 * @param x1 the x1 to set
	 */
	public void setX1(String[] x1) {
		this.x1 = x1;
	}

	/**
	 * @return the y1
	 */
	public String[] getY1() {
		return y1;
	}

	/**
	 * @param y1 the y1 to set
	 */
	public void setY1(String[] y1) {
		this.y1 = y1;
	}

	/**
	 * @return the x2
	 */
	public String[] getX2() {
		return x2;
	}

	/**
	 * @param x2 the x2 to set
	 */
	public void setX2(String[] x2) {
		this.x2 = x2;
	}

	/**
	 * @return the y2
	 */
	public String[] getY2() {
		return y2;
	}

	/**
	 * @param y2 the y2 to set
	 */
	public void setY2(String[] y2) {
		this.y2 = y2;
	}

	/**
	 * @return the href
	 */
	public String[] getHref() {
		return href;
	}

	/**
	 * @param href the href to set
	 */
	public void setHref(String[] href) {
		this.href = href;
	}

	/**
	 * @return the coTitle
	 */
	public String[] getCoTitle() {
		return coTitle;
	}

	/**
	 * @param coTitle the coTitle to set
	 */
	public void setCoTitle(String[] coTitle) {
		this.coTitle = coTitle;
	}

	/**
	 * @return the categoryMenu
	 */
	public Menu getCategoryMenu() {
		return categoryMenu;
	}

	/**
	 * @param categoryMenu the categoryMenu to set
	 */
	public void setCategoryMenu(Menu categoryMenu) {
		this.categoryMenu = categoryMenu;
	}

	/**
	 * @return the hao123
	 */
	public Hao123 getHao123() {
		return hao123;
	}

	/**
	 * @param hao123 the hao123 to set
	 */
	public void setHao123(Hao123 hao123) {
		this.hao123 = hao123;
	}

	/**
	 * @return the detailId
	 */
	public String getDetailId() {
		return detailId;
	}

	/**
	 * @param detailId the detailId to set
	 */
	public void setDetailId(String detailId) {
		this.detailId = detailId;
	}

	/**
	 * @return the startDateParam
	 */
	public String getStartDateParam() {
		return startDateParam;
	}

	/**
	 * @param startDateParam the startDateParam to set
	 */
	public void setStartDateParam(String startDateParam) {
		this.startDateParam = startDateParam;
	}

	/**
	 * @return the timestart
	 */
	public String getTimestart() {
		return timestart;
	}

	/**
	 * @param timestart the timestart to set
	 */
	public void setTimestart(String timestart) {
		this.timestart = timestart;
	}

	/**
	 * @return the endDateParam
	 */
	public String getEndDateParam() {
		return endDateParam;
	}

	/**
	 * @param endDateParam the endDateParam to set
	 */
	public void setEndDateParam(String endDateParam) {
		this.endDateParam = endDateParam;
	}

	/**
	 * @return the timeend
	 */
	public String getTimeend() {
		return timeend;
	}

	/**
	 * @param timeend the timeend to set
	 */
	public void setTimeend(String timeend) {
		this.timeend = timeend;
	}

	/**
	 * @return the imageSmall
	 */
	public File[] getImageSmall() {
		return imageSmall;
	}

	/**
	 * @param imageSmall the imageSmall to set
	 */
	public void setImageSmall(File[] imageSmall) {
		this.imageSmall = imageSmall;
	}

	/**
	 * @return the imageBig
	 */
	public File[] getImageBig() {
		return imageBig;
	}

	/**
	 * @param imageBig the imageBig to set
	 */
	public void setImageBig(File[] imageBig) {
		this.imageBig = imageBig;
	}

	/**
	 * @return the xmlResult
	 */
	public String getXmlResult() {
		return xmlResult;
	}

	/**
	 * @param xmlResult the xmlResult to set
	 */
	public void setXmlResult(String xmlResult) {
		this.xmlResult = xmlResult;
	}

	/**
	 * @return the imageSPath
	 */
	public String getImageSPath() {
		return imageSPath;
	}

	/**
	 * @param imageSPath the imageSPath to set
	 */
	public void setImageSPath(String imageSPath) {
		this.imageSPath = imageSPath;
	}

	/**
	 * @return the imageBPath
	 */
	public String getImageBPath() {
		return imageBPath;
	}

	/**
	 * @param imageBPath the imageBPath to set
	 */
	public void setImageBPath(String imageBPath) {
		this.imageBPath = imageBPath;
	}

	/**
	 * @return the xmlLastModify
	 */
	public String getXmlLastModify() {
		return xmlLastModify;
	}

	/**
	 * @param xmlLastModify the xmlLastModify to set
	 */
	public void setXmlLastModify(String xmlLastModify) {
		this.xmlLastModify = xmlLastModify;
	}

}
